package com.netease.videoLoaderDemo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.netease.neliveplayer.playerkit.videoLoader.OnVideoListScrollEventHandler;
import com.netease.neliveplayer.playerkit.videoLoader.VideoLoader;
import com.netease.neliveplayer.playerkit.videoLoader.VideoLoaderImpl;
import com.netease.videoLoaderDemo.adapter.RoomListAdapter;
import com.netease.videoLoaderDemo.service.MockItemImpl;
import com.netease.videoLoaderDemo.service.MockItemInterface;
import com.netease.videoLoaderDemo.service.VideoInfo;
import com.netease.videoSlidingKit.R;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;


public class RoomListActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private OnVideoListScrollEventHandler mOnVideoListScrollEventHandler;
    private RoomListAdapter mRoomAdapter;
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mSmartRefreshLayout;
    private MockItemInterface mMockDataProvider;
    private ArrayList<VideoInfo> mVideoInfoList;
    private ArrayList<SpriteImageInfo> mSpriteImageInfoList;
    boolean isFirstLoad = true;
    private ArrayList<VideoLoader.VideoInfo> mInfoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_room_list_activit);
        Intent intent = getIntent();
        int mode = intent.getIntExtra("mode", 0);
        mMockDataProvider = new MockItemImpl();
        mVideoInfoList = new ArrayList<>();
        mSpriteImageInfoList = new ArrayList<>();
        initUI();
        VideoLoaderImpl.getInstance().init(this, mode,null);
    }

    private void initUI() {
        mOnVideoListScrollEventHandler = new OnVideoListScrollEventHandler(){};
        mRoomAdapter = new RoomListAdapter(this);
        mRecyclerView = findViewById(R.id.rvRooms);
        mRecyclerView.setAdapter(mRoomAdapter);
        mRecyclerView.addOnScrollListener(mOnVideoListScrollEventHandler);
        mSmartRefreshLayout = findViewById(R.id.smart_refresh_layout);
        mSmartRefreshLayout.setEnableLoadMore(false);
        mSmartRefreshLayout.setEnableRefresh(true);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            mSmartRefreshLayout.setOnRefreshListener(refreshLayout -> mMockDataProvider.getRoomList(videoInfos -> {
                mVideoInfoList.clear();
                mVideoInfoList.addAll(videoInfos);
                if(isFirstLoad){
                    mInfoList = new ArrayList<>();
                    for(VideoInfo info: videoInfos){
                        VideoLoader.VideoInfo videoInfo = new VideoLoader.VideoInfo(info.vId, info.url);
                        mInfoList.add(videoInfo);
                    }
                    mOnVideoListScrollEventHandler.updateRoomList(mInfoList);
                    isFirstLoad = false;
                }
                updateList(videoInfos);
            }, spriteImageInfo -> {
                mSpriteImageInfoList.clear();
                mSpriteImageInfoList.addAll(spriteImageInfo);
                updateSpriteImageList(spriteImageInfo);
            }, e -> {

            }));
        }
        mSmartRefreshLayout.autoRefresh();
    }

    private void updateList(List<VideoInfo> videoInfos) {
        mRecyclerView.setVisibility(View.VISIBLE);
        mRoomAdapter.resetAll(videoInfos);
        mSmartRefreshLayout.finishRefresh();
    }
    private void updateSpriteImageList(List<SpriteImageInfo> SpriteImage) {
        mRoomAdapter.resetSpriteImageList(SpriteImage);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        VideoLoaderImpl.getInstance().release();
    }
    @Override
    protected void onResume() {
        super.onResume();
        if(mOnVideoListScrollEventHandler != null) {
              mOnVideoListScrollEventHandler.updateRoomList(mInfoList);
        }
    }


    public void startVideoItemActivity(ArrayList<VideoInfo> dataList, ArrayList<SpriteImageInfo> spriteInfoList, int position) {
        Intent intent = new Intent(this, VideoItemActivity.class);
        intent.putExtra("VideoInfoList", dataList);
        intent.putExtra("SpriteInfoList", spriteInfoList);
        intent.putExtra("VideoSeletedPosition", position);
        startActivity(intent);
    }
}